package com.team.mail.order.listener;


import com.rabbitmq.client.Channel;
import com.team.common.to.SeckillOrderTo;
import com.team.mail.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;



@RabbitListener(queues = "order.seckill.order.queue")
@Service
@Slf4j
public class OrderSeckillListener {
    @Autowired
    private OrderService orderService;

    //订单创建成功就发消息
    @RabbitHandler
    public void listener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {

        try {
            log.info ("准备创建秒杀的详细信息。。。");
            orderService.createSeckillOrder (seckillOrderTo);
            //手动调用支付宝的收单功能--->在线文档https://opendocs.alipay.com/apis/api_1/alipay.trade.close
            channel.basicAck (message.getMessageProperties ().getDeliveryTag (), false);
        } catch (Exception e) {
            //消息已拒绝、还返回到队列里面
            channel.basicReject (message.getMessageProperties ().getDeliveryTag (), true);
        }
    }
}
